<?php
	/**
	*Author 雷日锦 362606856@qq.com 
	*控制器自动生成
	*/
	if(!defined("ROOT_PATH")) exit("die Access ");
	class kcbackControl extends skymvc{
		public $tbs=array(
			1=>"ymdian",
			2=>"koudai"
		);
		public function __construct(){
			parent::__construct();
			
		}
		
		public function onDefault(){
			$typeid=max(1,get_post('typeid','i'));
			 
			$where="  kcid=".SHOPID;
			$url=APPKUCUN."?m=kcback&a=default&typeid=".$typeid;
			$status=get('status','i');
			$where.=" AND status=$status";
			$url.="&status=$status";
			$limit=50;
			$start=get("per_page","i");
		 
			$option=array(
				"start"=>intval(get_post('per_page')),
				"limit"=>$limit,
				"order"=>" order_id DESC",
				"where"=>$where
			);
			$rscount=true;
			$data=M("kcback")->select($option,$rscount);
			if($data){
				foreach($data as $k=>$v){
					$shopids[]=$v['shopid'];
					$siteids[]=$v['siteid'];
				}
				$shops=M($this->tbs[$v['typeid']])->getShopByIds($shopids);
			 	$sites=M("sites")->getByIds($siteids);
				foreach($sites as $s){
					$sitedata[$v['siteid']]['site']=$s;
				}
				
				foreach($data as $k=>$v){
					$shop=M($this->tbs[$v['typeid']])->selectRow(" id=".$v['shopid']);
					$v['num']=M("kcback_product")->getOne("select sum(buy_num) as ct from ".table("kcback_product")." where order_id=".$v['order_id']." ");
					$v['shop_name']=$shop['title'];
					$v['sitename']=$sites[$v['siteid']]['sitename'];
					$data[$k]=$v;
				}
				
			}
			if($data){
				foreach($data as $v){
					$sitedata[$v['siteid']]['orders'][]=$v;
				}
			}
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$order_status_list=$this->config_item('order_status_list');
			$this->smarty->assign(
				array(
					"data"=>$data,
					"sitedata"=>$sitedata,
					"typeid"=>$typeid,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"order_status_list"=>$order_status_list,
					"url"=>$url
				)
			);
			$this->smarty->display("kcback/index.html");
		}
		
		public function onShow(){
			$typeid=max(1,get_post('typeid','i'));
			$order_id=get_post("order_id","i");
			$data=M("kcback")->selectRow(array("where"=>"order_id={$order_id}"));
			if($data){
				$site=M("sites")->selectRow("siteid=".$data['siteid']);
				
				$shop=M($this->tbs[$data['typeid']])->selectRow(" id=".$data['shopid']);
				
				$order_product=M("kcback_product")->select(array(
					"where"=>"order_id=".$order_id
				));
				if($order_product){
					foreach($order_product as $k=>$v){
						$ids[]=$v['productid'];
					}
					$products=M("product")->getListByIds($ids);
				 	$total_num=0;
					foreach($order_product as $k=>$v){
						$v['title']=$products[$v['productid']]['title'];
						$v['imgurl']=$products[$v['productid']]['imgurl'];
						$v['p_price']=$products[$v['productid']]['price'];
						$total_num+=$v['buy_num'];
						$order_product[$k]=$v;
					}
					$data['total_num']=$total_num;
				}
			}
			$order_status_list=$this->config_item('order_status_list');
			$this->smarty->assign(array(
				"data"=>$data,
				"shop"=>$shop,
				"order_status_list"=>$order_status_list,
				"order_product"=>$order_product,
				"site"=>$site,
				"typeid"=>$typeid,
			));
			$this->smarty->display("kcback/show.html");
		}
		
		public function onpronum(){
		
			$order_id=get_post('order_id','i');
			$productid=get_post('productid','i');
			$order=M("kcback")->selectRow("order_id=".$order_id);
			if(empty($order) or $order['status']!=0){
				$this->goAll("参数出错",1);
			}
			$row=M("kcback_product")->selectRow("order_id=".$order_id." AND productid=".$productid."");
			$num=get('num','i');
			if($order['order_id']!=$row['order_id']){
				$this->goAll("参数出错",1);
			}
			if($row['buy_num']+$num<0){
				$this->goAll("产品若无，请取消订单",1);
			}
			
			$shop_product=M($this->tbs[$order['typeid']]."_product")->selectRow(" shopid=".$order['shopid']." AND productid=".$productid." ");
			if($num>$shop_product['total_num']){
				$this->goAll("退货数量超过店铺的库存，请减少",1);
			}
			M("kcback")->begin();
			M("kcback")->update(array(
				"money"=>$order['money']+$row['price']*$num,
				"gys_money"=>$order['gys_money']+$row['gys_price']*$num
			),"order_id=".$order_id);
			M("kcback_product")->update(array(
				"buy_num"=>$row['buy_num']+$num,
			),"id=".$row['id']);
			//产品
			M($this->tbs[$order['typeid']]."_product")->total_num_add(array(
				"shopid"=>$order['shopid'],
				"productid"=>$row['productid'],
				"total_num"=>-$num,
				"content"=>"[退货变更]" 
			));
			//发送推送信息
			$content=date("H:i:s")."您的退货订单".$order['orderno']."数量改变".$num."";
			M("apppush")->sendShop(array(
						"table"=>$this->tbs[$order['typeid']],
						"content"=>$content,
						"shopid"=>$order['shopid'],
					));
			//End 推送信息
			M("kcback")->commit();
			$this->goAll("成功了",0,array(
				"buy_num"=>$row['buy_num']+$num,
				"gys_money"=>$order['gys_money']+$row['gys_price']*$num,
				 "total_num"=> M("kcback_product")->selectOne(array(
					"where"=>"order_id=".$order_id,
					"fields"=>" sum(buy_num) as b"
				))
			));
		}
		
		
		/**
		*订单确认
		*/
		public function onConfirm(){
			$typeid=max(1,get_post('typeid','i'));
			$order_id=get_post('order_id','i');
			$data=M("kcback")->selectRow(array("where"=>"order_id=".$order_id));
			if(empty($data)) $this->goall("参数出错",1);
			M("kcback")->update(array("status"=>1),"order_id=".$order_id);
			$content=post('content');
			M("kcback_log")->insert(array(
				"dateline"=>time(),
				"admin_id"=>$this->admin['id'],
				"order_id"=>$order_id,
				"content"=>$content,
				"typeid"=>$data['typeid']
			));
			 
			$this->goall("确认成功");
		}
		/**
		*订单发送
		*/
		public function onSend(){
			$typeid=max(1,get_post('typeid','i'));
			$order_id=get_post('order_id','i');
			$data=M("kcback")->selectRow(array("where"=>"order_id=".$order_id));
			$kdy_telephone=get_post('kdy_telephone','h');
			if(empty($data)) $this->goall("参数出错",1);
			if($data['status']<2){
				M("kcback")->update(array("status"=>2),"order_id=".$order_id);
			}
			$content=post('content');
			$shipping_info=post('shipping_info','h');
			M("kcback_log")->insert(array(
				"dateline"=>time(),
				"admin_id"=>$this->admin['id'],
				"order_id"=>$order_id,
				"content"=>$content."快递信息：".$shipping_info,
				"typeid"=>$data['typeid']
			));
			M("kcback")->update(array("shipping_info"=>$shipping_info,"kdy_telephone"=>$kdy_telephone),"order_id=".$order_id);
			 
			$this->goall("发货成功");
		}
	
		/**
		*订单完成
		*/
		public function onFinish(){
			$this->goAll("暂无权限");
			$typeid=max(1,get_post('typeid','i'));
			$order_id=get_post('order_id','i');
			$data=M("kcback")->selectRow(array("where"=>"order_id=".$order_id));
			if(empty($data)) $this->goall("参数出错",1);
			if($data['isreceived']!=2){
				$this->goall("还未收货不能结单",1);
			}
			if($data['status']==3){
				$this->goAll("该订单已经完成",1);
			}
			M("kcback")->begin();
			M("kcback")->update(array("status"=>3),"order_id=".$order_id);
			$content=post('content');
			M("kcback_log")->insert(array(
				"dateline"=>time(),
				"admin_id"=>$this->admin['id'],
				"order_id"=>$order_id,
				"content"=>$content,
				"typeid"=>$data['typeid']
			));
			/*完成 加库存 减库存余额 加店铺余额
			$kucun=M("kucun")->selectRow("kcid=".$data['kcid']);
			$order_product=M("kcback_product")->select(array("where"=>"order_id=".$data['order_id']));
			foreach($order_product as $k=>$v){
				if($kucun['koudai']){
					M("koudai_product")->total_num_add( 
						array(
							"shopid"=>$kucun['koudai'],
							"productid"=>$v['productid']	,
							"total_num"=>$v['buy_num']
						)
					);
				}else{
					M('kucun_product')->changenum("total_num",$v['buy_num'],"productid=".$v['productid']." AND shopid=".$data['kcid']);
				}
			}
			//减去库存余额
			
			if($kucun){
				M("kucun")->addMoney(array(
					"shopid"=>$data['kcid'],
					"balance"=>-$data['gys_money'],
					"content"=>"您完成了退货订单".$data['orderno']."，余额减少[money]元"
				));
			}
			//加店铺余额
			
			M($this->tbs[$data['typeid']])->addMoney(array(
					"shopid"=>$data['shopid'],
					"balance"=>$data['money'],
					"content"=>"您的退货订单".$data['orderno']."完成了，余额增加[money]元"
			));
			**/
			/******库存 添加今日收入*****
			//退货减去金额
			$money1_b=$money1=-$data['gys_money'];
			$kday=date("Ymd");
			$rd=M("stat_shoporder_day")->selectRow(" shopid=".$data['kcid']." AND k='kucun' AND kday='".$kday."' ");
			if($rd){
				M("stat_shoporder_day")->update(array(
					"money"=>$money1+$rd['money'],
					"ordernum"=>$rd['ordernum']+1,
					"balance"=>$money1_b+$rd['balance']
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_day")->insert(array(
					"money"=>$money1,
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>1,
					"kday"=>$kday,
					"k"=>"kucun",
					"shopid"=>$data['kcid']
				));
			}
			//添加本周收入
			$kweek=date("YW");
			$rd=M("stat_shoporder_week")->selectRow(" shopid=".$data['kcid']." AND k='kucun' AND kweek='".$kweek."' ");
			if($rd){
				M("stat_shoporder_week")->update(array(
					"money"=>$money1+$rd['money'],
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>$rd['ordernum']+1
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_week")->insert(array(
					"money"=>$money1,
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>1,
					"kweek"=>$kweek,
					"k"=>"kucun",
					"shopid"=>$data['kcid']
				));
			}
			
			//添加本月收入
			$kmonth=date("Ym");
			$rd=M("stat_shoporder_month")->selectRow(" shopid=".$data['kcid']." AND k='kucun' AND kmonth='".$kmonth."' ");
			if($rd){
				M("stat_shoporder_month")->update(array(
					"money"=>$money1+$rd['money'],
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>$rd['ordernum']+1
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_month")->insert(array(
					"money"=>$money1,
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>1,
					"kmonth"=>$kmonth,
					"k"=>"kucun",
					"shopid"=>$data['kcid']
				));
			}
			*********End库存统计结束************/
			M("kcback")->commit(); 
			$this->goall("订单完成");
		}
		
		/**
		*取消订单
		*/
			
		public function onCancel(){
			
			$typeid=max(1,get_post('typeid','i'));
			$order_id=get_post('order_id','i');
			$data=M("kcback")->selectRow(array("where"=>"order_id=".$order_id));
			if(empty($data)) $this->goall("参数出错",1);
			if($data['status']>=3 or $data['isreceived']==2) $this->goall("操作失败",1);
			M("kcback")->begin();
			M("kcback")->update(array("status"=>10),"order_id=".$order_id);
			$content=post('content');
			$message=post('message','h');
			M("kcback_log")->insert(array(
				"dateline"=>time(),
				"admin_id"=>$this->admin['id'],
				"order_id"=>$order_id,
				"content"=>$content."，原因：".$message,
				"typeid"=>$data['typeid']
			));
			 
		 
			
			//添加产品销售数量
			$order_product=M("kcback_product")->select(array("where"=>"order_id=".$data['order_id']));
			foreach($order_product as $k=>$v){
				M($this->tbs[$data['typeid']].'_product')->total_num_add(array(
				"shopid"=>$v['shopid'],
				"productid"=>$v['productid'],
				"total_num"=>$v['buy_num'],
				"content"=>"[退货取消]"
			));			
			}
			M("kcback")->commit();
			$this->goall("取消成功");
		}
		
		
		public function onReceive(){
			
			$order_id=get('order_id','i');
			$data=M("kcback")->selectRow(array("where"=>" order_id=".$order_id." AND kcid=".SHOPID." "));
			if(empty($data) or $data['status']<2 or $data['isreceived']==2){
				$this->goall("暂时无法收货",1,0,"/index.php");
			}
			M("kcback")->begin();
			M("kcback")->update(array("isreceived"=>2)," order_id=".$order_id." ");
			$kucun=M("kucun")->selectRow("kcid=".$data['kcid']);
			$order_product=M("kcback_product")->select(array("where"=>"order_id=".$data['order_id']));
			foreach($order_product as $k=>$v){
				if($kucun['koudai']){
					M("koudai_product")->total_num_add(
						array(
							"shopid"=>$kucun['koudai'],
							"productid"=>$v['productid'],
							"total_num"=>$v['buy_num']
						)
					);
				}else{
					 
					M("kucun_product")->total_num_add(
						array(
							"shopid"=>$data['kcid'],
							"productid"=>$v['productid'],
							"total_num"=>$v['buy_num'],
							"content"=>"[退单完成]"
						)
				);
				}
			}
			
			/******库存 添加今日收入*****/
			//退货减去金额
			$money1_b=$money1=-$data['gys_money'];
			$kday=date("Ymd");
			$rd=M("stat_shoporder_day")->selectRow(" shopid=".$data['kcid']." AND k='kucun' AND kday='".$kday."' ");
			if($rd){
				M("stat_shoporder_day")->update(array(
					"money"=>$money1+$rd['money'],
					"ordernum"=>$rd['ordernum']+1,
					"balance"=>$money1_b+$rd['balance']
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_day")->insert(array(
					"money"=>$money1,
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>1,
					"kday"=>$kday,
					"k"=>"kucun",
					"shopid"=>$data['kcid']
				));
			}
			//添加本周收入
			$kweek=date("YW");
			$rd=M("stat_shoporder_week")->selectRow(" shopid=".$data['kcid']." AND k='kucun' AND kweek='".$kweek."' ");
			if($rd){
				M("stat_shoporder_week")->update(array(
					"money"=>$money1+$rd['money'],
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>$rd['ordernum']+1
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_week")->insert(array(
					"money"=>$money1,
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>1,
					"kweek"=>$kweek,
					"k"=>"kucun",
					"shopid"=>$data['kcid']
				));
			}
			
			//添加本月收入
			$kmonth=date("Ym");
			$rd=M("stat_shoporder_month")->selectRow(" shopid=".$data['kcid']." AND k='kucun' AND kmonth='".$kmonth."' ");
			if($rd){
				M("stat_shoporder_month")->update(array(
					"money"=>$money1+$rd['money'],
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>$rd['ordernum']+1
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_month")->insert(array(
					"money"=>$money1,
					"balance"=>$money1_b+$rd['balance'],
					"ordernum"=>1,
					"kmonth"=>$kmonth,
					"k"=>"kucun",
					"shopid"=>$data['kcid']
				));
			}
			/*********End库存统计结束************/
			M("kcback")->commit();
			$this->goall("已经确认收货",0,$data);
		
	}
		
		
		
	}

?>